#include "cdeviceinfo.h"

#include <Etel3rdParty.h>

CDeviceInfo* CDeviceInfo::NewL()
	{
	CDeviceInfo* self = NewLC();
	CleanupStack::Pop(self);
	return self;
	}

CDeviceInfo* CDeviceInfo::NewLC()
	{
	CDeviceInfo* self = new (ELeave) CDeviceInfo();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

void CDeviceInfo::ConstructL()
	{
	CActiveScheduler::Add(this);
	iTelephony = CTelephony::NewL();
	iWait = new (ELeave) CActiveSchedulerWait();
	}

CDeviceInfo::CDeviceInfo() :
	CActive(EPriorityNormal), iBatteryInfoV1Pkg(iBatteryInfoV1), iPhoneIdV1Pkg(
			iPhoneIdV1), iSignalStrengthV1Pckg(iSignalStrengthV1),
			iSubscriberIdV1Pckg(iSubscriberIdV1), iMCCMNCV1Pkg(iMCCMNCV1)
	{
	}

CDeviceInfo::~CDeviceInfo()
	{
	Cancel();
	delete iTelephony;
	}

TBuf<CTelephony::KPhoneSerialNumberSize> CDeviceInfo::imei()
	{
	Cancel();
	iTelephony->GetPhoneId(iStatus, iPhoneIdV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iPhoneIdV1.iSerialNumber;
	}

TBuf<CTelephony::KPhoneModelIdSize> CDeviceInfo::model()
	{
	Cancel();
	iTelephony->GetPhoneId(iStatus, iPhoneIdV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iPhoneIdV1.iModel;
	}

TBuf<CTelephony::KPhoneManufacturerIdSize> CDeviceInfo::manufacturer()
	{
	Cancel();
	iTelephony->GetPhoneId(iStatus, iPhoneIdV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iPhoneIdV1.iManufacturer;
	}

TBuf<CTelephony::KIMSISize> CDeviceInfo::imsi()
	{
	Cancel();
	iTelephony->GetSubscriberId(iStatus, iSubscriberIdV1Pckg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iSubscriberIdV1.iSubscriberId;
	}

TBuf<CTelephony::KNetworkShortNameSize> CDeviceInfo::operatorname()
	{
	Cancel();
	iTelephony->GetCurrentNetworkInfo(iStatus, iMCCMNCV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iMCCMNCV1.iShortName;
	}

TBuf<CTelephony::KNetworkCountryCodeSize> CDeviceInfo::mcc()
	{
	Cancel();
	iTelephony->GetCurrentNetworkInfo(iStatus, iMCCMNCV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iMCCMNCV1.iCountryCode;
	}

TBuf<CTelephony::KNetworkIdentitySize> CDeviceInfo::mnc()
	{
	Cancel();
	iTelephony->GetCurrentNetworkInfo(iStatus, iMCCMNCV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iMCCMNCV1.iNetworkId;
	}

TUint CDeviceInfo::lac()
	{
	Cancel();
	iTelephony->GetCurrentNetworkInfo(iStatus, iMCCMNCV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iMCCMNCV1.iLocationAreaCode;
	}

TUint CDeviceInfo::cid()
	{
	Cancel();
	iTelephony->GetCurrentNetworkInfo(iStatus, iMCCMNCV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iMCCMNCV1.iCellId;
	}

TUint CDeviceInfo::batteryLevel()
	{
	Cancel();
	iTelephony->GetBatteryInfo(iStatus, iBatteryInfoV1Pkg);
	SetActive();

	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iBatteryInfoV1.iChargeLevel;
	}

TInt32 CDeviceInfo::signalStrength()
	{
	Cancel();
	iTelephony->GetSignalStrength(iStatus, iSignalStrengthV1Pckg);
	SetActive();
	if (!iWait->IsStarted())
		{
		iWait->Start();
		}
	return iSignalStrengthV1.iSignalStrength;
	}

void CDeviceInfo::DoCancel()
	{
	iTelephony->CancelAsync(CTelephony::EGetPhoneIdCancel);
	}

void CDeviceInfo::RunL()
	{
	iWait->AsyncStop();
	}
